package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	music3 "gomuisc/service/music"
	"gomuisc/service/user"
	"net/http"
)
//中间件  跨域请求
func Cors() gin.HandlerFunc {
	return func(context *gin.Context) {
		method := context.Request.Method
		context.Header("Access-Control-Allow-Origin", "*")
		context.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
		context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
		context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		context.Header("Access-Control-Allow-Credentials", "true")
		if method == "OPTIONS" {
			context.AbortWithStatus(http.StatusNoContent)
		}
		context.Next()
	}
}
func JWTAuthMiddleware() func(c *gin.Context) {
	return func(c *gin.Context) {
		// 客户端携带Token有三种方式 1.放在请求头 2.放在请求体 3.放在URI
		// 这里假设Token放在Header的Authorization中，并使用Bearer开头
		// 这里的具体实现方式要依据你的实际业务情况决定
		token := c.Request.Header.Get("token")
		if token == "" {
			c.JSON(http.StatusOK, gin.H{
				"code": 2003,
				"msg":  "请求头中auth为空",
			})
			c.Abort()
			return
		}
		//// 按空格分割
		//parts := strings.SplitN(authHeader, " ", 2)
		//if !(len(parts) == 2 && parts[0] == "Bearer") {
		//	c.JSON(http.StatusOK, gin.H{
		//		"code": 2004,
		//		"msg":  "请求头中auth格式有误",
		//	})
		//	c.Abort()
		//	return
		//}
		// parts[1]是获取到的tokenString，我们使用之前定义好的解析JWT的函数来解析它
		mc, err := user.ParseToken(token)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 2005,
				"msg":  "无效的Token",
			})
			c.Abort()
			return
		}
		// 将当前请求的username信息保存到请求的上下文c上
		c.Set("user_id", mc.Id)
		fmt.Println(mc)
		fmt.Println(mc.Id)
		c.Next() // 后续的处理函数可以用过c.Get("username")来获取当前请求的用户信息
	}
}
func main()  {
	r  :=  gin.Default()
	r.Use(Cors())
	r.LoadHTMLFiles("template/music.html","template/login.html","template/register.html")
	r.Static("/dwz","template/statics")
	r.GET("music/searchMuisc", func(c *gin.Context) {
		c.HTML(http.StatusOK,"music.html",gin.H{
			"title": "音乐播放器",
		})
	})
	music := r.Group("/music")
	{
		music.GET("/search", music3.GetList)     //搜索歌曲  先分开后合并
		music.GET("/storeSearch", music3.GetStoreList)     //搜索库内歌曲
		music.GET("/comment", music3.GetComment) //获取歌曲评论
		music.GET("/sheetSearch", music3.SheetSearch)     //歌单搜歌曲
		//music.GET("/comment", music3.GetComment) //获取歌曲评论
		music.GET("/getSingerSingle",music3.GetSingerSingle)//通过歌手获取歌曲
		music.POST("/addSheet",JWTAuthMiddleware(),music3.AddSheet)//通过歌手获取歌曲
		music.POST("/getSheet",JWTAuthMiddleware(),music3.GetSheet)//通过歌手获取歌曲
		//music.GET("/getSingerInfo",music3.GetSingerInfo)//可能存在没有主页的情况 废弃
	}
	//QQ 授权接口
//http://127.0.0.1/toLogin
	r.GET("/toLogin", func(c *gin.Context) {
		c.HTML(http.StatusOK,"login.html",gin.H{
			"title": "音乐播放器",
		})
	})
	r.POST("/login", user.Login)
	r.GET("/toRegister",func(c *gin.Context) {
		c.HTML(http.StatusOK,"register.html",gin.H{
			"title": "音乐播放器",
		})
	})
	r.POST("/register", user.Register)
	//http.HandleFunc("/toLogin", func(writer http.ResponseWriter, request *http.Request) {
	//	fmt.Println(112323)
	//})
	//http.HandleFunc("/mycb", user.GetToken)
	r.Run (":80") // 在 0.0.0.0:8080 上监听并提供服务（对于 windows "localhost:808
}